package wykop.download;

import org.w3c.dom.Node;

import wykop.page.WykopPage;
import wykop.parser.PageNotParsed;
import wykop.parser.UserProvider;
import wykop.parser.WykopCommentParser;
import wykop.parser.WykopPageParser;

/**
 * Capable of downloading and parsing a page
 * 
 * @author kad
 * 
 */
public class PageDownloader {

	private final WykopPageParser pageParser;

	public PageDownloader(final UserProvider userProvider) {
		super();
		pageParser = new WykopPageParser(
				WykopCommentParser.getCommentParser(userProvider), userProvider);
	}

	public WykopPage download(final int i) throws DownloadingPageFailed,
			PageNotParsed {
		try {
			final Node node = WykopDownloader.get(i);
			final WykopPage page = pageParser.parse(i, node);
			System.out.println("Getting data for " + i + " went ok!");
			return page;
		} catch (final DownloadingPageFailedWith404 e) {
			return null;
		}

	}

}
